﻿CREATE PROCEDURE [acms].[SiteMapping_Put]
	@Id int, 
	@SiteId int,
	@MappingName varchar(255),
	@ConcurencyId rowversion,
	@IdOut int output,
	@ConcurencyIdOut rowversion output
AS

if exists(select null from SiteMapping_View with (nolock) where Id = @Id) begin
	update SiteMappings
	set
		SiteId = @SiteId
		, MappingName = @MappingName
	where
		Id = @Id
		and ConcurencyId = @ConcurencyId
	if	@@Rowcount = 1
		set @IdOut = @Id
	else
		return -100
end
else if @Id is null or @Id <= 0 begin
	insert SiteMappings
		(SiteId, MappingName)
	values
		(@SiteId, @MappingName)
	set @IdOut = @@Identity
end
else begin
	return -200
end

select @ConcurencyIdOut = ConcurencyId from SiteMappings with (nolock) where Id = @IdOut